package com.example.owner.lettery1.other;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/29.
 */
public abstract class TextTableAdapter<T> extends BaseAdapter {
    private List<T> mData;
    private Context mContext;
    private TextItemCreator mItemCreator;

    public void setItemCreator(TextItemCreator itemCreator) {
        mItemCreator = itemCreator;
    }

    public TextTableAdapter(Context context) {
        mContext = context;
        mData = new ArrayList<>();
    }

    public void setData(List<T> data) {
        mData.clear();
        addData(data);
    }

    public void addData(List<T> data) {
        mData.addAll(data);
        notifyDataSetChanged();
    }

    public void clearData() {
        mData.clear();
        notifyDataSetChanged();
    }

    public TextItemCreator getItemCreator() {
        return mItemCreator;
    }

    public List<T> getData() {
        return mData;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextTableViewHolder holder;
        if (convertView == null) {
            holder = mItemCreator.createItem();
            convertView = holder.getRowLayout();
            convertView.setTag(holder);
        } else {
            holder = (TextTableViewHolder) convertView.getTag();
        }
        return convert(holder, convertView, position);
    }

    public abstract View convert(TextTableViewHolder holder, View convertView, int position);

    @Override
    public T getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

}
